package com.smartrm.globalinfrcore.cmd.interceptor;

import com.smartrm.globalinfrcore.api.CommonError;
import com.smartrm.globalinfrcore.cmd.AbstractCommandContext;
import com.smartrm.globalinfrcore.cmd.Command;
import com.smartrm.globalinfrcore.cmd.CommandConfig;
import com.smartrm.globalinfrcore.exception.DomainException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.stereotype.Component;

/*
 * @program: common-starters
 * @description: 处理CommandContext, 负责校验CommandContext
 * @author: houhong
 * @create: 2023-01-11 22:14
 */
@Component
@AutoConfigureAfter(InvokerInterceptor.class)
public class CommandContextInterceptor implements CommandInterceptor {

    @Autowired
    private InvokerInterceptor invokerInterceptor;

    @Override
    public CommandInterceptor getNext() {
        return this.invokerInterceptor;
    }

    @Override
    public void setNext(CommandInterceptor next) {

    }

    /*
     * 校验上下文 是否为空，上下文为空 直接返回
     */
    @Override
    public <T> T execute(AbstractCommandContext commandContext, CommandConfig config, Command<T> command) throws Exception {

        if (null == commandContext) {
            throw new DomainException(CommonError.NO_EXSIT_COMMAND_CONTEXT);
        }

        return this.invokerInterceptor.execute(commandContext, config, command);
    }
}